package com.briup.smartcity_group1.web.controller.auth;

import com.briup.smartcity_group1.bean.basic.Article;
import com.briup.smartcity_group1.consts.BeanNameConsts;
import com.briup.smartcity_group1.consts.DataMessageConsts;
import com.briup.smartcity_group1.service.IArticleService;
import com.briup.smartcity_group1.utils.result.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**资讯信息控制器
 * @author congee(congee02 @ 163.com)
 * @date 1/5/2023 1:31 AM
 */
@RestController
@RequestMapping(BeanNameConsts.ARTICLE)
public class ArticleController {

    @Autowired
    private IArticleService service;

    @PutMapping("status")
    public void changeArticleStatus(int id, Integer status) throws IllegalAccessException {
        service.changeArticleStatus(id, status);
        Result.setDataMessage(DataMessageConsts.ARTICLE_STATUS_CHANGED);
    }

    @GetMapping
    public Article findArticleById(@RequestParam(required = true) int id) {
        return service.findArticleById(id);
    }

    @GetMapping("page/query/recommend")
    public PageInfo<Article> findAllRecommendedArticlesByPage
            (@RequestParam(required = true) int page, @RequestParam(required = true) int pageSize) {
        return service.findAllRecommendedArticlesByPage(page, pageSize);
    }

    @GetMapping("page/categoryAndUser")
    public PageInfo<Article> findAllArticlesByPage(@RequestParam(required = false) String title,
                                                   @RequestParam(required = false) Integer categoryId,
                                                   @RequestParam(required = false) Integer status,
                                                   @RequestParam(required = true) int pageNum,
                                                   @RequestParam(required = true) int pageSize) {
        return service.findAllArticlesByPage(title, categoryId, status, pageNum, pageSize);
    }

    @PostMapping(consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public void saveOrUpdateArticle(Article article) {
        service.saveOrUpdateArticle(article);
        Result.setDataMessage(DataMessageConsts.ARTICLE_SAVE_OR_UPDATE_SUCCESS);
    }



}
